package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.mapper.DtsPermissionMapper;
import com.qf.pojo.DtsRole;
import com.qf.service.DtsPermissonService;
import com.qf.service.DtsRolesService;
import com.qf.util.*;
import com.qf.vo.PermVo;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/admin/role")
@CrossOrigin("http://localhost")
@Slf4j
public class AdminRoleController {
    @Autowired
    DtsRolesService rolesService;
    @Autowired
    ApplicationContext context;
    @Autowired
    DtsPermissonService permissionService;
    @PostMapping("/permissions")
    public Object UpdatePermissions(@RequestBody Map<String,Object> map){
        Integer roleId =(Integer) map.get("roleId");
        List<String> permissions =(List) map.get("permissions");
        //如果是超级管理员
        if(rolesService.checkIsSuperManager(roleId)){
            return AdminResponseUtil.fail(AdminResponseCode.ROLE_SUPER_SUPERMISSION);
        }
        //不是超级管理员
        rolesService.UpdatePermissions(roleId,permissions);
        return ResponseUtil.ok();

    }
    @GetMapping("/permissions")
    public Object permissions(Integer roleId){
        //获取系统权限：解析controller中的每个方法上的注解，获取系统权限
        List<Permission> permissions = PermissionUtil.listPermission(context, "com.qf.controller");
        List<PermVo> permVos = PermissionUtil.listPermVo(permissions);
        Set<String>stringSet=null;
        if(rolesService.checkIsSuperManager(roleId)){
            stringSet = PermissionUtil.listPermissionString(permissions);
        }else{
            stringSet = permissionService.findByRoleIds(new Integer[]{roleId});
        }
        Map<String,Object>data=new HashMap<>();
        data.put("systemPermissions",permVos);
        data.put("assignedPermissions",stringSet);
        return ResponseUtil.ok(data);
    }
    @RequiresPermissions("admin:role:delete")
    @RequiresPermissionsDesc(menu = { "系统管理", "角色管理" }, button = "角色删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsRole role){
        rolesService.delete(role.getId());
        log.info("删除成功");
        return ResponseUtil.ok();
    }
    @RequiresPermissions("admin:role:update")
    @RequiresPermissionsDesc(menu = { "系统管理", "角色管理" }, button = "角色编辑")
    @PostMapping("/update")
    public Object update(@RequestBody DtsRole role){
        DtsRole role1=rolesService.updateById(role);

        return ResponseUtil.ok(role1);
    }
    @RequiresPermissions("admin:role:create")
    @RequiresPermissionsDesc(menu = { "系统管理", "角色管理" }, button = "角色添加")
    @GetMapping("/create")
    public Object create(@RequestBody DtsRole role){
       Boolean flag =rolesService.checkRole(role.getName());
       if(flag){
           return AdminResponseUtil.fail(AdminResponseCode.ROLE_NAME_EXIST);
       }
        DtsRole role1=rolesService.insert(role);
        return ResponseUtil.ok(role1);
    }
    @RequiresPermissions("admin:role:list")
    @RequiresPermissionsDesc(menu = { "系统管理", "角色管理" }, button = "角色查询")
    @GetMapping("/list")
    public Object queryPage(
            @RequestParam(value="page" ,required=false,defaultValue ="1" )Integer page,
            @RequestParam(value="limit",required=false,defaultValue ="10" )Integer limit,
            String rolename,String sort,String order){
        IPage<DtsRole> roles= rolesService.selectByPage(page,limit,rolename,sort,order);
        Map<String,Object> data=new HashMap<>();

        data.put("total",roles.getTotal());
        data.put("items",roles.getRecords());



        return ResponseUtil.ok(data);
    }
    @GetMapping("/options")
    public Object options(){
        List<DtsRole> dtsRoles=rolesService.findAll();
        List<Map<String, Object>> data = dtsRoles.stream().map(role -> {
            Map<String, Object> map = new HashMap<>();
            map.put("value", role.getId());
            map.put("label", role.getName());
            return map;
        }).collect(Collectors.toList());

        return ResponseUtil.ok(data);
    }

}
